package com.climber.spb_rabbitmq.spb_delayqueue_plugin.controller;

import com.climber.spb_rabbitmq.spb_delayqueue_plugin.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class MessageController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 发布消息
     * 以队列为单位设置消息的延时
     *
     * @param content
     */
    @GetMapping("/sendMessageDelay/{content}/{time}")
    public void sendMessageDelay(@PathVariable("content") String content,@PathVariable("time") Integer ttlTime) {
        System.out.println(new Date().toString());
        rabbitTemplate.convertAndSend(RabbitMQConfig.DELAYED_EXCHANGE_NAME, RabbitMQConfig.DELAYED_ROUTING_KEY, content, correlationData -> {
            correlationData.getMessageProperties().setDelay(ttlTime);
            return correlationData;
        });
    }


}